package cn.ac.big.bigd.gsa.service;

import cn.ac.big.bigd.gsa.dao.GsaDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangfan on 2017/8/8.
 */
@Service
public class GsaServive {
    @Autowired
    GsaDao gsaDao;
    @Value("${app.gsaApiHost}")
    private String gsaApiHost;

    public Map findBySample(String accession){
        RestTemplate template = new RestTemplate();
       Map result = template.getForObject(gsaApiHost+"/getSample/{accession}", Map.class, accession);
       return result;
    }



    public Map findBySampleWithResult(String accession){
        Map m =new HashMap();
        RestTemplate template = new RestTemplate();
        Map result = template.getForObject(gsaApiHost+"/getSample/{accession}", Map.class, accession);
        Map taxon = (Map) result.get("taxon");

        m.put("scientificName",taxon.get("name").toString());
        m.put("commonName",taxon.get("commonNames").toString());
        m.put("taxonId",taxon.get("taxonId").toString());
        m.put("sampleName",result.get("name").toString());
        return m;
    }


    public   String getDirBySampleWithResult(String accession){

        RestTemplate template = new RestTemplate();
        Map result = template.getForObject(gsaApiHost+"/getSample/{accession}", Map.class, accession);
        Map taxon = (Map) result.get("taxon");
        List<Map> ancestors = (ArrayList<Map>)taxon.get("ancestors");

        for (int i = 0; i < ancestors.size(); i++) {
            Map  m =ancestors.get(i);
            if(m.get("ranks").toString().equals("superkingdom")){

                if(m.get("name").toString().equals("Eukaryota")){

                    for (int j = i; j < ancestors.size(); j++) {
                        Map child = ancestors.get(j + 1);
                        if (child.get("ranks").toString().equals("kingdom")) {

                            if(child.get("name").toString().equals("Metazoa")){
                                return "Animals";
                            }else if(child.get("name").toString().equals("Viridiplantae")){
                                return "Plants";
                            }else{
                                return child.get("name").toString();
                            }


                        }
                    }
                }else{
                    return m.get("name").toString();
                }
            }
            
        }
        return  "Others";
    }






    public   String getLineageBySampleWithResult(String accession){
        RestTemplate template = new RestTemplate();
        Map result = template.getForObject(gsaApiHost+"/getSample/{accession}", Map.class, accession);
        Map taxon = (Map) result.get("taxon");
        List<Map> ancestors = (ArrayList<Map>)taxon.get("ancestors");
        List<String> stringList =new ArrayList<>();

        for (int i = 0; i < ancestors.size(); i++) {
            Map  m =ancestors.get(i);
            stringList.add(m.get("name").toString());

        }
        stringList.add(taxon.get("name").toString());

       return  StringUtils.join(stringList, ";");

    }


}
